package com.nuatar.meibi;


import java.text.SimpleDateFormat;
import java.util.Date;

import com.nuatar.meibi.pubvar.LAdapter;
import com.nuatar.meibi.pubvar.LAdapter.ViewBinder;
import com.nuatar.meibi.pubvar.MyMsg;
import com.nuatar.meibi.pubvar.NetBus;
import com.nuatar.meibi.pubvar.PhoneMaster;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MessageCenter extends FatherActivity {
	ListView lv;
	LAdapter la;
	RelativeLayout rla;
	RelativeLayout rld;
	class MHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			if(msg.what==1){
				la.notifyDataSetChanged();
				rld.setVisibility(View.GONE);
				rla.setVisibility(View.GONE);
			}else if(msg.what==2){
				rld.setVisibility(View.GONE);
				rla.setVisibility(View.VISIBLE);
			}
		}
	}
	public static MHandler mhandler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message_center);
		mhandler=new MHandler();
		rla=(RelativeLayout) findViewById(R.id.NoMsg);
		rld=(RelativeLayout) findViewById(R.id.loadBar);
		rla.setVisibility(View.GONE);
		final MeiBi mks=(MeiBi) this.getApplication();
		lv=(ListView) findViewById(R.id.MsgList);
		ViewBinder vb=new ViewBinder(){

			@Override
			public View getView(int position, Context ctx, Object data) {
				// TODO Auto-generated method stub
				TextView tv=new TextView(ctx);
				tv.setTextSize(20);
				tv.setTextColor(Color.BLACK);
				tv.setBackgroundResource(android.R.drawable.editbox_dropdown_light_frame);
				tv.setText(getShort(((MyMsg)data).info));
				return tv;
			}

			@Override
			public View update(View v, int position, Object data) {
				// TODO Auto-generated method stub
				return null;
			}};
		la=new LAdapter(this.getApplicationContext(), null, vb);
		lv.setAdapter(la);
		lv.setDividerHeight(15);
		lv.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				mks.Msg=(MyMsg) la.mlist.get(arg2);
				Intent i=new Intent();
				i.setClass(getApplicationContext(), MsgShow.class);
				startActivity(i);
			}});
		MT mt=new MT();
		new Thread(mt).start();
		
	}
	public String getShort(String info){
		if(info.length()>10){
			return info.substring(0, 10);
		}else{
			return info;
		}
	}
	@Override
	protected void onStart() {
		super.onStart();
		mGaTracker.sendView(this.toString());
	}
	class MT implements Runnable{
		@Override
		public void run() {
			// TODO Auto-generated method stub
			SharedPreferences pre = PreferenceManager
					.getDefaultSharedPreferences(getApplicationContext());
			String MsgKey="MsgTime";
			String msgtime=pre.getString(MsgKey, "20130101000000");
			Date tm = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			String time = sdf.format(tm);
			SharedPreferences.Editor edt=pre.edit();
			edt.putString(MsgKey, time);
			edt.commit();
			TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
			String did = PhoneMaster.getMD5Str(telephonyManager.getDeviceId());
			la.mlist=NetBus.getMessage(did,msgtime);
			if(la.mlist!=null&&la.mlist.size()>0){
				MessageCenter.mhandler.sendEmptyMessage(1);
			}else{
				MessageCenter.mhandler.sendEmptyMessage(2);
			}
		}
		
	}

}
